package com.ruoyi.mdm.service.impl;

import com.ruoyi.mdm.domain.SkuMaster;
import com.ruoyi.mdm.domain.SkuMergeMaster;
import com.ruoyi.mdm.domain.SkuProductMaster;
import com.ruoyi.mdm.mapper.SkuMasterMapper;
import com.ruoyi.mdm.mapper.SkuMergeMasterMapper;
import com.ruoyi.mdm.mapper.SkuProductMasterMapper;
import com.ruoyi.mdm.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * sku同步ES 定时任务Service业务层处理
 *
 * @author shiseido
 * @date 2025-03-01
 */
@Service
public class SkuToEsTaskServiceImpl implements ISkuToEsTaskService {

    @Autowired
    private SkuMasterMapper skuMasterMapper;

    @Autowired
    private SkuMergeMasterMapper skuMergeMasterMapper;

    @Autowired
    private ISkuMasterService skuMasterService;

    @Autowired
    private ISkuMergeMasterService skuMergeMasterService;

    @Autowired
    private SkuProductMasterMapper skuProductMasterMapper;

    @Autowired
    private ISkuProductMasterService skuProductMasterService;

    /**
     * SKU同步到ES
     */
    @Override
    public void syncSkuToEs() {

        // 全量同步SKU TO ES
        syncGoodsToEs();

        // 全量同步新增ES-SKUM
        syncSkumToEs();

        // 全量同步新增ES-SPU
        syncSpuToEs();
    }

    @Async
    public void syncGoodsToEs() {
        // 全量同步SKU TO ES
        SkuMaster skuSelectMaster = new SkuMaster();
        List<SkuMaster> skuMasterList = skuMasterMapper.selectSkuMasterToEs(skuSelectMaster);
        if(!skuMasterList.isEmpty()){
            skuMasterService.syncSkuToEs(skuMasterList, false);
        }
    }

    @Async
    public void syncSkumToEs() {
        // 全量同步SKU TO ES
        SkuMergeMaster skuMergeMaster = new SkuMergeMaster();
        List<SkuMergeMaster> skuMergeMasterList = skuMergeMasterMapper.selectSkuMergeMasterList(skuMergeMaster);
        if(!skuMergeMasterList.isEmpty()){
            skuMergeMasterService.syncSkumToEs(skuMergeMasterList);
        }

    }

    @Async
    public void syncSpuToEs() {
        // 全量同步SKU TO ES
        SkuProductMaster skuProductMaster = new SkuProductMaster();
        List<SkuProductMaster> skuProductMasterList = skuProductMasterMapper.selectSkuProductMasterList(skuProductMaster);
        skuProductMasterService.syncSpuToEs(skuProductMasterList);
    }

}
